'use strict';

const BitArray = require('node-bitarray');
const _ = require('lodash');

class Pack {
  constructor({ id, version, mode, dataType, data, dataHex, dataLength }) {
    this.id = id;
    this.version = version;
    this.mode = mode;
    this.dataType = dataType;
    this.data = data;
    this.dataHex = dataHex;
    this.dataLength = dataLength;
  }

  handle(terminalId, app) {
    console.log(`data from ${terminalId}`);
  }
}

class StatusPack extends Pack {
  constructor(args) {
    super(Object.assign({
      id: 1,
      mode: 7,
      dataLength: 1,
      dataType: 5,
    }, args));
  }

  static req() {
  }

  static res({ version, dataHex }) {
    return new StatusPack({
      version,
      dataHex,
      data: parseInt(dataHex, '16'),
    });
  }
}

class WorkTimePack extends Pack {
  constructor(args) {
    super(_.extend({
      id: 4,
      dataLength: 4,
      dataType: 2,
    }, args));
  }

  static req(data) {
    let dataHex = data.toString('16');

    dataHex = _.repeat('0', 8 - dataHex.length) + dataHex;
    return new WorkTimePack({
      version: '55AA00',
      mode: 6,
      data,
      dataHex,
    });
  }

  static res({ dataHex }) {
    return new WorkTimePack({
      version: '55AA01',
      mode: 7,
      dataHex,
      data: parseInt(dataHex, 16),
    });
  }
}

module.exports = {
  1: StatusPack,
  4: WorkTimePack,
};
